﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using QLDAOL.ModelView;
using QLDAOL.Util;
using QLDAOL.Models;
namespace QLDAOL.Controllers
{
    public class InBoxController : BaseController
    {

        private IEQLDAOLRepository db = new IEQLDAOLRepository();
        private static  int userlogin = 0;
        //
        // GET: /InBox/
        [HttpGet]
        public ActionResult InBox()
        {
            if (Session["user_id"] != null && int.Parse(Session["user_id"].ToString()) != 0)
            {
                ViewData["userid"] = Session["user_id"];
                userlogin = int.Parse(Session["user_id"].ToString());
            }
            else
                return RedirectIndex();
            return View();
        }
        public ActionResult MailInbox()
        {
            IEnumerable<MAIL> inbox = db.getMailByUser(userlogin);
            ViewData["Mail"] = inbox;
            return View();
        }
        [HttpGet]
        public ActionResult MailContent(int id)
        {
            MAIL Mail = db.getMailByMailid(id);
            ViewData["Title"]=Mail.Title;
            ViewData["SendID"] = db.getUserByUserid((int)Mail.ID_UserCreated).UserName;
            ViewData["ReciveID"] = db.getUserByUserid((int)Mail.ID_UserReceived).UserName;
            ViewData["Des"] =Server.HtmlDecode( Mail.Description);
            ViewData["Date"] = ((DateTime)Mail.Date_Created).ToShortDateString();
            return View();
        }
        [HttpPost, ValidateInput(false)]
        public ActionResult MailContent(InBoxModel mail)
        {
            if (!ModelState.IsValid)
            {
                return View();
            }
            int id = userlogin;
            var newemail = new MAIL
            {

                Seen = false,
                ID_UserCreated = id,
                Type = 1,
                Title = mail.title,
                Description = Server.HtmlEncode(mail.Description),
                ID_UserReceived = db.getUserId(mail.username),
                Date_Created = DateTime.Now
            };
            db.addMail(newemail);
            return RedirectToAction("InBox");
        }
        public ActionResult sendmail()
        {

           return  RedirectToAction("Inbox");
        }
        [HttpGet]
        public ActionResult newmail()
        {

            return View();
        }
        [HttpPost, ValidateInput(false)]
        public ActionResult newmail(InBoxModel mail)
        {
            if (!ModelState.IsValid)
            {
                return View();
            }
            int id= userlogin;
            var newemail = new MAIL
            {
        
                Seen= false,
                ID_UserCreated = id,
                Type=1,
                Title= mail.title,
                Description=Server.HtmlEncode( mail.Description),
                ID_UserReceived= db.getUserId(mail.username),
                Date_Created= DateTime.Now
            };
            db.addMail(newemail);
            return RedirectToAction("InBox");
        }
        public ActionResult sendedmail()
        {
            IEnumerable<MAIL> outbox = db.getSentMailByUser(userlogin);
            ViewData["Mail"] = outbox;
            return View();
        }

    }
}
